package com.gexiaobao.pigeon.ui.dialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.text.method.ScrollingMovementMethod
import android.util.DisplayMetrics
import android.view.Display
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.*
import com.gexiaobao.pigeon.R
import me.hgj.jetpackmvvm.base.appContext

/**
 * Author: liuqiang
 * Time: 2018-01-02 13:28
 * Description:
 */
class AlertDialog(private val context: Context) {
    var dialog: Dialog? = null
    private var lLayout_bg: LinearLayout? = null
    var txt_title: TextView? = null
    private var txt_msg: TextView? = null
    private var btn_neg: Button? = null
    private var btn_pos: Button? = null
    private var img_line: ImageView? = null
    private val display: Display
    private val real_display: Display? = null
    private var showTitle = false
    private var showMsg = false
    private var showPosBtn = false
    private var showNegBtn = false
    private val outMetrics: DisplayMetrics

    init {
        val windowManager = context
            .getSystemService(Context.WINDOW_SERVICE) as WindowManager
        display = windowManager.defaultDisplay
        outMetrics = DisplayMetrics()
        windowManager.defaultDisplay.getRealMetrics(outMetrics)
    }

    @SuppressLint("InflateParams")
    fun builder(): AlertDialog {
        // 获取Dialog布局
        val view = LayoutInflater.from(context).inflate(R.layout.view_alertdialog, null)

        // 获取自定义Dialog布局中的控件
        lLayout_bg = view.findViewById<View>(R.id.lLayout_bg) as LinearLayout
        txt_title = view.findViewById<View>(R.id.txt_title) as TextView
        txt_title!!.visibility = View.GONE
        txt_msg = view.findViewById<View>(R.id.txt_msg) as TextView
        txt_msg!!.movementMethod = ScrollingMovementMethod.getInstance()
        txt_msg!!.visibility = View.GONE
        btn_neg = view.findViewById<View>(R.id.btn_neg) as Button
        btn_neg!!.visibility = View.GONE
        btn_pos = view.findViewById<View>(R.id.btn_pos) as Button
        btn_pos!!.visibility = View.GONE
        img_line = view.findViewById<View>(R.id.img_line) as ImageView
        img_line!!.visibility = View.GONE


        // 定义Dialog布局和参数
        dialog = Dialog(context, R.style.AlertDialogStyle)
        dialog!!.setContentView(view)

        // 调整dialog背景大小
        lLayout_bg!!.layoutParams = FrameLayout.LayoutParams((outMetrics.widthPixels * 0.75).toInt(), LinearLayout.LayoutParams.WRAP_CONTENT)
        return this
    }

    fun setTitle(title: String): AlertDialog {
        showTitle = true
        if ("" == title) {
            txt_title!!.text = appContext.getString(R.string.title)
        } else {
            txt_title!!.text = title
        }
        return this
    }

    fun setMsg(msg: String): AlertDialog {
        showMsg = true
        if ("" == msg) {
            txt_msg!!.text = appContext.getString(R.string.content)
        } else {
            txt_msg!!.text = msg
        }
        return this
    }

    fun setCancelable(cancel: Boolean): AlertDialog {
        dialog!!.setCancelable(cancel)
        return this
    }

    fun setTitleLongListener(listener: View.OnLongClickListener): AlertDialog {
        if (showTitle) {
            txt_title!!.setOnLongClickListener { v ->
                listener.onLongClick(v)
                false
            }
        }
        return this
    }

    fun setPositiveButton(text: String, listener: View.OnClickListener): AlertDialog {
        showPosBtn = true
        if ("" == text) {
            btn_pos!!.text = appContext.getString(R.string.sure)
        } else {
            btn_pos!!.text = text
        }
        btn_pos!!.setOnClickListener { v: View? ->
            listener.onClick(v)
            dialog!!.dismiss()
        }
        return this
    }

    fun setNegativeButton(text: String, listener: View.OnClickListener): AlertDialog {
        showNegBtn = true
        if ("" == text) {
            btn_neg!!.text = appContext.getString(R.string.cancel)
        } else {
            btn_neg!!.text = text
        }
        btn_neg!!.setOnClickListener { v ->
            listener.onClick(v)
            dialog!!.dismiss()
        }
        return this
    }

    private fun setLayout() {
        if (!showTitle && !showMsg) {
            txt_title!!.text = appContext.getString(R.string.reminder)
            txt_title!!.visibility = View.VISIBLE
        }
        if (showTitle) {
            txt_title!!.visibility = View.VISIBLE
        }
        if (showMsg) {
            txt_msg!!.visibility = View.VISIBLE
        }
        if (!showPosBtn && !showNegBtn) {
            btn_pos!!.text = appContext.getString(R.string.sure)
            btn_pos!!.visibility = View.VISIBLE
            btn_pos!!.setBackgroundResource(R.drawable.alertdialog_single_selector)
            btn_pos!!.setOnClickListener { v: View? -> dialog!!.dismiss() }
        }
        if (showPosBtn && showNegBtn) {
            btn_pos!!.visibility = View.VISIBLE
            btn_pos!!.setBackgroundResource(R.drawable.alertdialog_right_selector)
            btn_neg!!.visibility = View.VISIBLE
            btn_neg!!.setBackgroundResource(R.drawable.alertdialog_left_selector)
            img_line!!.visibility = View.VISIBLE
        }
        if (showPosBtn && !showNegBtn) {
            btn_pos!!.visibility = View.VISIBLE
            btn_pos!!.setBackgroundResource(R.drawable.alertdialog_single_selector)
        }
        if (!showPosBtn && showNegBtn) {
            btn_neg!!.visibility = View.VISIBLE
            btn_neg!!.setBackgroundResource(R.drawable.alertdialog_single_selector)
        }
    }

    fun show() {
        setLayout()
        dialog!!.show()
    }
}